#ifndef DEMUX_HANDLER
#define DEMUX_HANDLER

#include "tools.h"
#include "demuxer.h"
#include "thread_handler.h"
#include <string>
#include "decode_handler.h"
#include <iostream>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
namespace lcf
{

    enum XSYN_TYPE
    {
        XSYN_NONE = 0,  // 不做同步
        XSYN_VIDEO = 1, // 根据视频同步，不处理音频
    };

    class DemuxHandler : public ThreadHandler
    {
    public:
        DemuxHandler() : timeoutMs_(0), syncType_(XSYN_NONE) {}
        ~DemuxHandler() {}
        bool Seek(long long ms);

        /// 音频索引
        int AudioIndex() { return demuxer_.AudioIndex(); }
        /// 视频索引
        int VideoIndex() { return demuxer_.VideoIndex(); }

        /// 入口线程函数
        void Main() override;

        /// 打开解封装
        bool Open(std::string url, int timeoutMs = 1000);

        // 复制视频参数
        std::shared_ptr<ParameterManager> CopyVideoPara() { return demuxer_.CopyVideoPara(); }
        std::shared_ptr<ParameterManager> CopyAudioPara() { return demuxer_.CopyAudioPara(); }
        /// 设置同步类型，只支持视频
        void SetSyncType(XSYN_TYPE syncType) { syncType_ = syncType; }

        /// 停止线程并清理资源，需要Wait等待线程结束
        void Stop();

    private:
        Demuxer demuxer_;
        std::string url_;
        int timeoutMs_; // 超时时间
        XSYN_TYPE syncType_;
    };

}

#endif
